package br.ufrj.dcc.trabxml.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.Vector;

import javax.swing.JOptionPane;

import br.ufrj.dcc.trabxml.manager.BookManager;
import br.ufrj.dcc.trabxml.model.TLivro;
import br.ufrj.dcc.trabxml.view.FormMain;

public class FindBooks implements ActionListener {

    private FormMain form = null;

    public FindBooks(FormMain form) {
        this.form = form;
    }

    public void actionPerformed(ActionEvent e) {
        String searchString = form.getSearchString();
        BookManager manager = new BookManager();
        Collection<TLivro> books = new Vector<TLivro>();
        try {
            if (form.getSearchGroup() == form.getMyGroup()) {
                books = manager.listBooks(searchString);
            } else {
                books = manager.listRemoteBooks(form.getSearchGroup(),searchString);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(form, e1.getMessage(), "Erro",
                    JOptionPane.ERROR_MESSAGE);      
            return;
        }
        manager.populateImageCache(books);
        form.setBooksNumber(books.size());
        Collection<TLivro> sortedBooks = new TreeSet<TLivro>(new Comparator<TLivro>() {

			public int compare(TLivro arg0, TLivro arg1) {
                int ret = arg0.getTitulo().compareToIgnoreCase(arg1.getTitulo());
                if (ret != 0) {
                    return ret;
                }
                ret = arg0.getIsbn().compareToIgnoreCase(arg1.getIsbn());
                if (ret != 0) {
                    return ret;
                }                
				ret = arg0.getDono().compareTo(arg1.getDono());			
				return ret;
			}
        	
        });
        sortedBooks.addAll(books);
        String xml = null;
        try {
            xml = manager.getControlXML();
        } catch (Exception e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(form, e1.getMessage(), "Erro",
                    JOptionPane.ERROR_MESSAGE);  
            return;
        }
        try {
            for (TLivro book : sortedBooks) {
                int from = manager.getLastGroup(xml,book.getDono().intValue(), book.getIsbn());
                int to = manager.getActualGroup(xml,book.getDono().intValue(), book.getIsbn());
                form.addBook(book.getDono().toString(), book.getIsbn(), book
                        .getTitulo(), book.getEditora(), book.getAutor(), book
                        .getObs(),from,to);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(form, e1.getMessage(), "Erro",
                    JOptionPane.ERROR_MESSAGE);  
            return;            
        }
        form.focusSearch();
        
    }

}
